const util = require("../../utils/util")

const DB = wx.cloud.database().collection('user_list')
const app = getApp()
Page({
  data: {
    isUser: '',
    logged: false,
    function: [{
        text: '任务记录',
        icon: '../../icon/socord.png',
      },
      {
        text: '任务提醒',
        icon: '../../icon/feedback.png',
      },
      {
        text: '专注模式',
        icon: '../../icon/focuses.png',
      },
      {
        text: '静态时钟',
        icon: '../../icon/time1.png',
      },
      {
        text: '白噪音',
        icon: '../../icon/noise.png',
      },
      {
        text: '记事本',
        icon: '../../icon/notes.png',
      },
    ]
  },
  //登陆出发事件
  getUserProfile() {
    let that = this
    let openid  = null
    //获取授权
    wx.getUserProfile({
      desc: '用于展示信息',
      success: res => {
        app.globalData.userInfo = res.userInfo
        wx.setStorageSync('userInfo', res.userInfo)
        //获取用户openid
        wx.cloud.callFunction({
          name: "getOpenid",
          success(res) {
            console.log(res.result.openid);
            openid = res.result.openid;
            //查找用户是否存在
            wx.showToast({
              title: '登陆中',
              icon: 'loading',
              mask: true
            })
            wx.cloud.callFunction({
              name: "test",
              success: res => {
                if (res.result.data.length > 0) {
                  DB.where({
                    "_openid": openid,
                  })
                  .update({
                    data:{
                      "isLoged": true
                    }
                  })
                  util.chenageLogin();
                  wx.showToast({
                    title: '登陆成功',
                    icon: 'success',
                    duration: 2000
                  })
                  console.log('查找成功');
                  wx.switchTab({
                    url: '../index/index',
                  })
                } else {
                  DB.where({
                    "_openid": openid,
                  })
                  .update({
                    data:{
                      "isLoged": true
                    }
                  })
                  util.chenageLogin();
                  let userInfo = wx.getStorageSync('userInfo');
                  let login = {"isLoged":false}
                  userInfo = util.extend(userInfo,login);
                  that.addUser(userInfo)
                  wx.switchTab({
                    url: '../index/index',
                  })
                }
              },
              fail: err => {
                console.log('查找失败', err);
              }
            })
          },
          fail(err) {
            console.log("获取用户openid失败", err);
          }
        })
      },
      fail: err => {
        console.log('未授权', err);
        wx.showToast({
          title: '请授权登陆使用',
          duration: 2000,
          icon: 'none'
        })
      }
    })
  },
  //获取用户openid
  getOpendid() {
    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        //查找用户是否存在
        wx.showToast({
          title: '登陆中',
          icon: 'loading',
          mask: true
        })
        DB.where({
            "_openid": res.result.openid
          })
          .get()
          .then(res => {
            if (res.data.length == 0) {
              wx.showToast({
                title: '登陆成功',
                icon: 'success',
                duration: 2000
              })
              console.log(res);
              app.globalData.userInfo = res.userInfo
              console.log(res.userInfo);
              let login = {"isLoged":false}
              // let user = res.userInfo
              wx.setStorageSync('userInfo', res.userInfo)
              that.addUser(res.userInfo)
              wx.switchTab({
                url: '../index/index',
              })

              console.log("获取用户信息失败", res)
              wx.showToast({
                title: '请授权登陆使用',
                duration: 2000,
                icon: 'none'
              })
            } else {
              wx.showToast({
                title: '登陆成功',
                icon: 'success',
                duration: 2000
              })
              app.globalData.userInfo = res.data[0]
              wx.setStorageSync('userInfo', res.data[0])
              wx.switchTab({
                url: '../index/index',
              })
            }
          })
          .catch(res => {
            console.log("查找失败", res);
          })
      },
      fail(res) {
        console.log("获取用户openid失败", res);
      }
    })
    let that = this
    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        console.log(that.findUser(res.result.openid));
        return that.findUser(res.result.openid);
      },
      fail(res) {
        console.log("获取用户openid失败", res);
      }
    })
  },
  //查找用户是否存在
  findUser(openid) {
    let that = this
    wx.showToast({
      title: '登陆中',
      icon: 'loading',
      mask: true
    })
    DB.where({
        "_openid": openid
      })
      .get()
      .then(res => {
        console.log(res.data.length);
        if (res.data.length == 0) {
          // wx.getSetting({
          //   success: res => {
          // if (!res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          return true;

          // } else {
          // wx.openSetting({
          //   success: res => {
          //     console.log(res)
          //   },
          //   fail: res => {
          //     console.log(res)
          //   }
          // })
          // wx.showToast({
          //   title: '请授权登陆使用',
          //   duration:2000,
          //   icon: 'none'
          // })
          // }
          // }
          // })
        } else {
          return false;
          wx.showToast({
            title: '登陆成功',
            icon: 'success',
            duration: 2000
          })
          app.globalData.userInfo = res.data[0]
          wx.setStorageSync('userInfo', res.data[0])
          wx.switchTab({
            url: '../index/index',
          })
        }
      })
      .catch(res => {
        console.log("查找失败", res);
      })
  },
  //添加新用户

  addUser(user) {
    DB
      .add({
        data: user
      })
      .then(res => {
        console.log("添加成功", res);
      })
      .catch(res => {
        console.log("添加失败", res);
      })
  },

  onLoad: function (options) {
    
  },
  onShow :function(){

  }
})